package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;


    /**
     * 加入购物车
     * @param num
     * @param id
     * @return
     */
    @RequestMapping("/add")
    public Result addCart(Integer num, Long id) {
        String username = tokenDecode.getUsername();
        cartService.add(id, num, username);
        return new Result(true, StatusCode.OK, "加入购物车成功");
    }

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public Result list() {
        String username = tokenDecode.getUsername();
        List<OrderItem> list = cartService.list(username);
        return new Result(true, StatusCode.OK, "购物车列表查询成功", list);
    }
}
